﻿using IR.Common.Utilities;
using IR.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IR.Controller.ModelControllers
{
    public class CNote
    {
        private MNote _currentNote;

        #region **Initializers**
        /// <summary>
        /// New Idea
        /// </summary>
        public CNote()
        {
            _currentNote = new MNote();
        }

        /// <summary>
        /// For edit screens
        /// </summary>
        /// <param name="cNoteToCopy">Push the CNote to copy</param>
        public CNote(CNote cNoteToCopy)
        {
            if (cNoteToCopy != null)
            {
                // Have to load because We can't set IdeaID
                _currentNote = MNote.Load(cNoteToCopy.NoteID);
            }
            else
            {
                _currentNote = new MNote();
            }
        }

        /// <summary>
        /// For existing Notes
        /// If note doesn't exit, it will throw an exception
        /// </summary>
        /// <param name="noteIDToFind">Enter the noteID to search for</param>
        public CNote(int noteIDToFind)
        {
            _currentNote = MNote.Load(noteIDToFind);
        }

        /// <summary>
        /// Used for Controller constructors
        /// </summary>
        /// <param name="mIdeaToCopy">Pass in the MIdea to copy</param>
        internal CNote(MNote mIdeaToCopy)
        {
            _currentNote = mIdeaToCopy;
        }
        #endregion

        #region **Model Relations**
        public int NoteID
        {
            get { return _currentNote.NoteID; }
        }

        // Validation in MUser & CUser
        public CUser UserCreated
        {
            get { return new CUser(_currentNote.User); }
            set
            {
                MUser temp = MUser.Load(value.Username);

                if (value != null && temp != null)
                    _currentNote.User = temp;
            }
        }

        public DateTime DateCreated
        {
            get { return _currentNote.DateCreated; }
            set
            {
                if (value != null && value > System.DateTime.Now)
                    _currentNote.DateCreated = value;
            }
        }

        public string Notes
        {
            get { return _currentNote.Notes; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                _currentNote.Notes = value;
            }
        }

        public CIdea Idea
        {
            get { return new CIdea(_currentNote.Idea); }
            set
            {
                MIdea temp = MIdea.Load(value.IdeaID);

                if (value != null && temp != null)
                    _currentNote.Idea = temp;
            }
        }
        #endregion

        /// <summary>
        /// ***ALWAYS wrap this in a try catch... if it fails it will throw an exceiption***
        /// 
        /// Runs a query on the user table based on a Query you pass in
        /// !!!NOTE: Pass in null to get everything
        /// </summary>
        /// <param name="queries">Dictonary... key = column name, value = value in column</param>
        /// <returns>List of CNotes matching all params, all null param</returns>
        public static List<CNote> QueryNotes(Query queries)
        {
            // Pass in a non-null dictionary
            if (queries == null)
                queries = new Query();

            List<MNote> modelList = MNote.QueryNotes(queries.Queries);

            // Return an empty List if none
            if (modelList == null)
                modelList = new List<MNote>();

            List<CNote> returnList = new List<CNote>(modelList.Count);

            foreach (MNote currentUser in modelList)
            {
                returnList.Add(new CNote(currentUser));
            }

            return returnList;
        }
    }
}
